package formatters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import localization.Localization;
import presentation.*;
import templates.PresentationTemplate;
import templates.PropertyTemplate;
import utils.GenericPair;

public class ViewFormatter implements Formatter {

    private String output;
    private int captureResource = 0;

    @Override
    public String format(PresentationElement e) {
        e.acceptView(this);
        return output;
    }

    public void formatText(TextElement e) {
        output = e.getValue();
    }

    public void formatPhone(PhoneElement e) {
        PresentationTemplate pt = e.getTemplate();
        PropertyTemplate template = pt.getPropertyTemplate();
        String value = (template.isNullEnabled() && e.getValue().equals(pt.getNullValue())) ? Localization.getString(pt.getNullValue()) : e.getValue();
        output = "<img src=\"phone.png\" class=\"medium\" />" + value;
    }

    public void formatEmail(EmailElement e) {
        PresentationTemplate pt = e.getTemplate();
        PropertyTemplate template = pt.getPropertyTemplate();
        String value = (template.isNullEnabled() && e.getValue().equals(pt.getNullValue())) ? Localization.getString(pt.getNullValue()) : e.getValue();
        output = "<img src=\"email.png\" class=\"medium\" />" + value;
    }

    private String unescapeHtml(String s) {
        return s.replaceAll("\"", "&quot;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\'", "&quot;");
    }

    public void formatLink(LinkElement e) {
        if (captureResource == 0) {
            captureResource = e.getUid() + e.hashCode();
        }
        int localResource = e.getUid() + e.hashCode();
        boolean captureTooltip = localResource == captureResource;

        String out = "";
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder("<table><tr>");
        List<GenericPair<PresentationElement, Boolean>> list = (List<GenericPair<PresentationElement, Boolean>>) e.getParam(LinkElement.GET_EXTERNALS);
        for (int i=0; i<list.size(); i++) {
            if (list.get(i).getSecond()) {
                sb.append(format(list.get(i).getFirst())).append(" ");
                if (captureTooltip)
                    sb2.append("<td>").append(format(list.get(i).getFirst())).append("</td>");
            } else {
                if (captureTooltip)
                    sb2.append("<td>").append(format(list.get(i).getFirst())).append("</td>");
            }
        }
        if (sb.toString().isEmpty()) {
            PresentationTemplate pt = e.getTemplate();
            PropertyTemplate template = e.getPropertyTemplate();
            out = (template.isNullEnabled() && e.getValue().equals(pt.getNullValue())) ? Localization.getString(pt.getNullValue()) : e.getValue();
        } else
            out = sb.toString();

        sb2.append("</tr></table>");
        if (captureTooltip) {
            String toolTipMessage = unescapeHtml(sb2.toString());
            output = "<div class=\"linkElement\" onmouseover=\"tooltip(this, \'" + toolTipMessage + "\'); \" onmouseout=\"hide_info(this); \">" + out + "</div>";
        } else
            output = out;
        if (captureTooltip) {
            captureResource = 0;
        }
    }

    public void formatCurrency(CurrencyElement e) {
        output = "<img src=\"money_dollar.png\" class=\"small\" />" + e.getValue();
    }

    public void formatDate(DateElement e) {
        try {
            DateElement date = (DateElement) e;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
            Date d = sdf.parse(date.getValue());
            sdf.applyPattern("dd.MM.yyyy");
            output = "<img src=\"calendar.png\" class=\"medium\" />" + sdf.format(d);
        } catch (ParseException ex) {
        }
    }
}
